package com.example.anew.journey.mynewjourney.logic.thirdPartyLogin;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.BaseRegisteredAndLoginActivity;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.logic.getVersion.GetVersionActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.bean.GetVersionBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司13
 * Created by 20433 on 2018/3/6
 *
 * @desc 第三方登陆
 */

public class ThirdPartyLoginActivity extends BaseRegisteredAndLoginActivity {

    @Bind(R.id.activity_third_party_login_tv_dec)
    TextView activityThirdPartyLoginTvDec;
    @Bind(R.id.activity_third_party_login_iv_phone_picter)
    ImageView activityThirdPartyLoginIvPhonePicter;
    @Bind(R.id.activity_third_party_login_et_input_phone_number)
    EditText activityThirdPartyLoginEtInputPhoneNumber;
    @Bind(R.id.linearLayout5)
    LinearLayout linearLayout5;
    @Bind(R.id.activity_third_party_login_bt_get_version_code)
    Button activityThirdPartyLoginBtGetVersionCode;

    public String third_party_login_id;
    public String third_party_login_name;
    public String third_party_login_state;
    /**
     * @desc 初始化数据 onCreate()
     * */
    @Override
    protected void initView(View view) {
        // todo Toolbar的一系列操作
        setToolbarBg(0xffffffff);
        Intent intent = getIntent();
        third_party_login_id = intent.getStringExtra("third_party_login_id");
        third_party_login_name = intent.getStringExtra("third_party_login_name");
        third_party_login_state = intent.getStringExtra("third_party_login_state");
        NLogger.e("TAG","=========state=====1001===="+third_party_login_id);
        NLogger.e("TAG","=========state=====1002===="+third_party_login_name);
        NLogger.e("TAG","=========state=====1003===="+third_party_login_state);


        activityThirdPartyLoginBtGetVersionCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String phone_number =  activityThirdPartyLoginEtInputPhoneNumber.getText().toString().trim();
                if(TextUtils.isEmpty(phone_number)){
                    Toast.makeText(ThirdPartyLoginActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
                }else{
                    erificationvMobile(phone_number);
                }
            }
        });
        //findRegisteredOrLoginView(view);
    }

    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        View view = View.inflate(this, R.layout.activity_third_party_login, null);
        ButterKnife.bind(this,view);
        return view;
    }
    /**
     * @desc Activity 销毁 释放资源
     */
    @Override
    protected void releaseResources() {
        ButterKnife.unbind(this);
    }


    private void getVersionCode(final String phone_number) {
        String url = RBConstants.SERVER_URL+RBConstants.REGISTERED_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", phone_number)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        final GetVersionBean getVersion = gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200){
                                    // todo 跳转页面输入验证码的页面

                                   Intent intent =  new Intent(ThirdPartyLoginActivity.this,GetVersionActivity.class);
                                    intent.putExtra("third_party_login_state",third_party_login_state);  //Third party login
                                    intent.putExtra("third_party_login_id",third_party_login_id);  //Third party login
                                    intent.putExtra("third_party_login_name",third_party_login_name);  //Third party login
                                    intent.putExtra("phone",phone_number);  //Third party login
                                    NLogger.e("TAG","=========state=====1004===="+third_party_login_id);
                                    NLogger.e("TAG","=========state=====1005===="+third_party_login_name);
                                    NLogger.e("TAG","=========state=====1006===="+third_party_login_state);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                                    startActivity(intent);
                                    finish();
                                    Toast.makeText(ThirdPartyLoginActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(ThirdPartyLoginActivity.this,getVersion.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                        return null;
                    }
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(ThirdPartyLoginActivity.this,"验证码发送失败，请重新发送",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    public   void erificationvMobile(String phoneNumber) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (phoneNumber.length() != 11) {
            // System.out.println("手机号应为11位数");
            Toast.makeText(this,"手机号应为11位数",Toast.LENGTH_SHORT).show();
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phoneNumber);
            boolean isMatch = m.matches();
            if (isMatch) {
                getVersionCode(phoneNumber);
            } else {
                // System.out.println("您的手机号" + phoneNumber + "格式错误");
                Toast.makeText(this,"手机号格式错误",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NLogger.e("TAG","=========222=fgcj===========");
    }
    }


